package cn.lls.base.common;

import cn.lls.base.result.R;
import cn.lls.base.result.ResultCode;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截器中未通过权限校验使用的工具类，用于返回前端
 *
 * @ClassName UnauthResult
 * @Author 划水艺术家
 * @Date 2022/11/19 23:14
 * @Version 1.0
 **/
public class UnAuthResult {

    /**
     * preHandle 返回 false.
     * 返回权限错误消息
     */
    public static void returnUnauthorized(HttpServletResponse response, ResultCode resultCode) throws IOException {
        returnUnauthorized(response, resultCode.getCode(), resultCode.getMessage());
    }

    /**
     */
    public static void returnUnauthorized(HttpServletResponse response, Integer code, String message) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(R.fail(code, message)));
    }
}
